<template>
  <div class="form-wrap">
    <div class="clearfix title">
      <span class="h3">管理员</span>
      {{ title }}
    </div>
    <keep-alive>
      <router-view
        :table-title="tableTitle"
        :table-data="tableData"
        :create-path="createPath"
        :default-operate="admin"
        :get-table-list="getTableList"
        @childTitle="childTitle"
        @edit="edit"
        @del="del"
        @selectionChange="selectionChange"/>
    </keep-alive>
  </div>

</template>

<script>
  import auth from '@/mixins/auth'

export default {
  data() {
    return {
      title: '',
      createPath: 'system_manage.user.create',
      tableTitle: [
        {
          cn: 'id',
          en: 'id'
        },
        {
          cn: '用户名',
          en: 'username'
        },
        {
          cn: '名称',
          en: 'name'
        },
        {
          cn: '角色',
          en: 'rolename'
        },
        {
          cn: '创建时间',
          en: 'created_at'
        },
        {
          cn: '更新时间',
          en: 'updated_at'
        }
      ],
      tableData: []
    }
  },
  mixins: [auth],
  methods: {
    selectionChange(val) {
      // console.log(val);
    },

    childTitle(val) {
      this.title = val
    },
    del(val) {
      console.log(val, 'del')
      this.$post('action/user/del', { id: val.row.id })
        .then(res => {
          this.tableData.splice(val.$index, 1)
          return this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
    },
    getTableList(data, pageInfo) {
      const loading = this.$loading()
      this.$get('action/user/dataGrid', data).then(res => {
        console.log(res)
        const list = res.data.list
        pageInfo.totalPage = res.data.totalPage
        pageInfo.totalRow = res.data.totalRow

        this.tableData = list
        loading.close()
      })
        .catch(_=>{
          loading.close()
        })
    },
    edit(val) {
      console.log(1)
      this.$router.push({
        name: 'system_manage.user.edit',
        params: {
          data: val.row
        }
      })
    }
  }
}
</script>

<style scoped rel="stylesheet/scss" lang="scss">
    .form-wrap {
        padding: 0 15px;

        .title {
            padding: 15px 0;
        }
    }
</style>
